<template>
  <div class="com-container">
    <div class="com-echart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  data() {
    return {
      echartInstance: null,
      // 服务器返回的数据
      allData: null,
      currentPage: 1,
      totalPage: 0, //一共有多少页
      timerId: null
    };
  },
  computed:{
    ...mapState(['theme']),
  },
  watch:{
    theme(){
      // 销毁当前图表
      this.echartInstance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener('resize', this.screenAdapter);
    // 首次适配
    this.screenAdapter();
  },
  beforeDestroy() {
    if (this.timerId) {
      clearInterval(this.timerId);
      this.timerId = null;
    }
    // 移除resize事件
    window.removeEventListener('resize', this.screenAdapter);
  },
  methods: {
    // 初始化echartsInstance对象
    initChart() {
      this.echartInstance = this.$echarts.init(this.$refs.seller_ref, this.theme);
      // 对图表初始化配置的控制
      const initOption = {
        title:{
          text: '▎商家销售统计',
          textStyle: {
            fontSize: 66
          },
          // 配置标题的位置
          left: 20,
          top: 20
        },
        // 网格
        grid:{
          // 网格的位置
          top: '20%',
          left: '3%',
          right: '6%',
          bottom: '3%',
          // 设置的位置是包含坐标轴上的文字的
          containLabel: true
        },
        xAxis: {
          type: "value"
        },
        yAxis: {
          type: "category"
        },
        tooltip:{
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            // 设置层级
            z: 0,
            lineStyle: {
              width: 66,
              color: '#2D3443'
            }
          }
        },
        series: [
          {
            type: "bar",
            // 柱的宽度
            barWidth: 66,
            label: {
              show: true,
              position: 'right',
              textStyle:{
                color: 'white'
              }
            },
            // 控制柱中的每一项
            itemStyle: {
              // 设置四个角的圆形
              barBorderRadius: [0, 33, 33, 0],
              // 指明颜色渐变的方向
              // 指明不同百分比之下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0 ,0, 1, 0, [
                // 百分之0状态下的颜色
                {
                  offset: 0,
                  color: '#5052EE'
                },
                // 百分之百状态下的颜色
                {
                  offset: 1,
                  color: '#AB6EE5'
                }
              ])
            }
          }
        ]
      };
      this.echartInstance.setOption(initOption);
      // 对图表进行鼠标事件监听
      this.echartInstance.on('mouseover',() => {
        console.log('mouseover');
        clearInterval(this.timerId);
        this.timerId = null;
      });
      this.echartInstance.on('mouseout',() => {
        console.log('mouseout');
        this.startInterval();
      });
    },
    // 获取服务器数据
    async getData() {
      const { data: ret } = await this.$http.get("seller");
      this.allData = ret;
      // 对数组进行排序
      this.allData.sort((a, b) => {
        return a.value - b.value; //从小到大的排序,反之则从大到小
      });
      // 每5个元素显示一页
      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;

      this.updateChart();
      // 调用定时器
      this.startInterval();
    },
    // 更新图表
    updateChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const shoWData = this.allData.slice(start, end);
      const sellerNames = shoWData.map(v => v.name);
      const sellerValues = shoWData.map(v => v.value);

      const dataOption = {
        yAxis: {
          data: sellerNames
        },
        series: [
          {
            data: sellerValues
          }
        ]
      };
      this.echartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
        this.timerId = null;
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPage) {
          this.currentPage = 1;
        }
        this.updateChart();
      }, 3000);
    },
    // 图表对屏幕的适配
    screenAdapter(){
      const titleFontSize = this.$refs.seller_ref.offsetWidth / 100 * 3.6;
      const adapterOption = {
        title:{
          textStyle: {
            fontSize: titleFontSize
          },
          // 配置标题的位置
          left: 20,
          top: 20
        },
        tooltip:{
          axisPointer: {
            lineStyle: {
              width: titleFontSize
            }
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            }
          }
        ]
      };
      this.echartInstance.setOption(adapterOption);
      // 手动调用图表对象的resize以适配
      this.echartInstance.resize();
    }
  }
};
</script>

<style lang="less" scoped></style>
